package net.comm.param.bean;

import java.sql.Connection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;

import net.comm.db.DBProcException;
import net.comm.param.db.CnpcaServiceSqlMysql;
import net.comm.param.db.DbCnpcaService;
import net.comm.util.DbcpPool;

import org.apache.log4j.Logger;
import org.sakaiproject.db.impl.BasicSqlService;


public class CnpcaBean {

	static Logger logger = Logger.getLogger(CnpcaBean.class.getName());
	
	public CnpcaBean() {
		super();
		// TODO Auto-generated constructor stub
		Idmap = new HashMap<Integer, String>();
	}
	private  HashMap<Integer, String> Idmap;
	
	private int  		Id;		//序号
	private String  	Name;	//名称
	private int  		Pid;	//省ID
	private int  		Cid;	//市ID

	//根据名称取对应的ID
	public int getID(String name) {
		
		Connection cn = null;
		try
		{
			Id = 0;
			if(name == null ||name.equals(""))
			{
				return Id;
			}
			//判断AssortIDmap中是否存在，存在则返回
			for (Iterator<Integer> i = Idmap.keySet().iterator(); i.hasNext();) { 
				int key = i.next(); 
				String value = Idmap.get(key); 
				if(value.equals(name)){
					Id = key;
					break;
				}
			} 			
			if(Id  > 0)
			{
				return Id;
			}

			cn = DbcpPool.getConnection();
			//取得FORM中传入的查询参数
			Object[] fields2 = new Object[1];
			fields2[0] = name;
			
			CnpcaServiceSqlMysql servSql = new CnpcaServiceSqlMysql();
			BasicSqlService  bSqlServ = new BasicSqlService();
			
			String sql = servSql.getRecbyNameSql();			
					
			List list = bSqlServ.dbRead(cn, sql, fields2, new DbCnpcaService());
			if(list.size() == 0){

				DbcpPool.closeTran(cn);
				DbcpPool.returnConnection(cn);
				return 0;				
			}
			BaseCnpca base = (BaseCnpca)list.get(0);
			
			Id = base.getId();
			Name = base.getName();
			
			Idmap.put(Id, Name);
			
			DbcpPool.returnConnection(cn);
		} catch(DBProcException ex) 
		{
			logger.info(ex.getMessage());
			System.out.println(ex.getMessage());
		} catch(Exception ex) 
		{
			logger.info(ex.getMessage());
			System.out.println(ex.getMessage());
		} finally {
			DbcpPool.returnConnection(cn);
		}	
		
		return Id;
	}
	//根据ID取对应的名称
	public String getName(int id) {
		
		Name = "";
		
		Connection cn = null;
		try
		{
			if(id == 0)
			{
				return Name;
			}
			if(Name.equals("")){
				return Name;
			}
			
			//判断hashmap中是否存在，存在则返回
			Name = Idmap.get(String.valueOf(id));
			if(Name != null && !Name.equals(""))
			{
				return Name;
			}		
			cn = DbcpPool.getConnection();

			//取得FORM中传入的查询参数
			Object[] fields2 = new Object[1];
			
			fields2[0] = id;
			
			CnpcaServiceSqlMysql servSql = new CnpcaServiceSqlMysql();
			BasicSqlService  bSqlServ = new BasicSqlService();
			
			String sql = servSql.getbykeySql();			
					
			List list = bSqlServ.dbRead(cn, sql, fields2, new DbCnpcaService());
			if(list.size() == 0){

				DbcpPool.closeTran(cn);
				DbcpPool.returnConnection(cn);
				return "";				
			}
			BaseCnpca base = (BaseCnpca)list.get(0);
				
			Id = base.getId();
			Name = base.getName();
			
			Idmap.put(Id, Name);
				
			DbcpPool.returnConnection(cn);
			
		} catch(DBProcException ex) 
		{
			logger.info(ex.getMessage());
		} catch(Exception ex) 
		{
			logger.info(ex.getMessage());
		} finally {
			DbcpPool.returnConnection(cn);
		}	
		
		return Name;
	}	
	
}
